import sys
sys.path.append("..")
import json
from flask import request
from create_id import get_id
from db_client import *

def transferContent(content):
        if content is None:
            return None
        else:
            string = ""
            for c in content:
                if c == '"':
                    string += '\\\"'
                elif c == "'":
                    string += "\\\'"
                elif c == "\\":
                    string += "\\\\"
                else:
                    string += c
            return string

def store_error(e):
    # 存储入库单
    e = str(e)
    e = transferContent(e)
    _id = get_id('ERROR')

    flag2 = mysql_client.store_error(_id, e)
    
    if flag2:
        t = {
            'state': 'false',
            'msg': '错误信息已存入数据库'
        }

        return json.dumps(t, ensure_ascii=False)

    else:
        t = {
            'state': 'false',
            'msg': '恭喜你，发现了一个bug。'
        }

        return json.dumps(t, ensure_ascii=False)


if __name__ == '__main__':
    _id = get_id('ERROR')
    #e = "".join('the JSON object must be str, not 'bytes')
    flag2 = mysql_client.store_error(_id, e)
    print(flag2)